package com.cat.dataStructure02;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/stable-subarrays-with-equal-boundary-and-interior-sum/
 * @create 2025/10/26 20:11
 * @since JDK17
 */

public class Solution66 {
    record Pair(Integer a, Long b) {}
    public long countStableSubarrays(int[] capacity) {
        Map<Pair, Integer> map = new HashMap<>();
        long sum = capacity[0], ans = 0;
        int n = capacity.length;
        for (int i = 1; i < n; i++) {
            ans += map.getOrDefault(new Pair(capacity[i], sum), 0);
            map.merge(new Pair(capacity[i - 1], capacity[i - 1] + sum), 1, Integer::sum);
            sum += capacity[i];
        }

        return ans;
    }
}
